package com.kob.botrunningsystem.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * @author molimark<br />
 * @date: 2023/4/3 16:41<br/>
 * @description: <br/>
 */

//这个代码将来由前端传过来
public class AIBot implements java.util.function.Supplier<Integer>{
    private int[] dx = {-1,0,1,0};
    private int[] dy = {0,1,0,-1};

    @Override
    public Integer get() {
        File file = new File("input.txt");
        try {
            Scanner sc = new Scanner(file);
            return nextMove(sc.next());
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    static class Cell {
        public int x,y;
        public Cell(int x,int y){
            this.x = x;
            this.y = y;
        }
    }

    private boolean check_tail_increasing(int step){ // 检验当前回合，蛇的长度是否增加
        if(step <= 10)return true;
        if(step % 3 == 1)return true;
        return false;
    }

    public List<Cell> getCells(int sx,int sy,String steps){
        steps = steps.substring(1,steps.length()-1);
        List<Cell> res = new LinkedList<>();

        int[] dx = {-1,0,1,0};
        int[] dy = {0,1,0,-1};

        int x = sx, y = sy;
        int step=0;

        res.add(new Cell(x,y));
        for(int i=0;i<steps.length();i++){
            int d = steps.charAt(i)-'0';
            x+=dx[d];
            y+=dy[d];
            if(!check_tail_increasing(++step)){
                res.remove(0);
            }
            res.add(new Cell(x,y));
        }

        return res;
    }

    private int dfs(int x,int y,int[][] g){
        if(x<0||x>=13||y<0||y>=14||g[x][y]==1)return 0;
        g[x][y]=1;
        int res = 1;
        for(int i=0;i<4;i++){
            int nx = x+dx[i],ny = y+dy[i];
            res += dfs(nx,ny,g);
        }
        return res;
    }

    public Integer nextMove(String input) {
        String[] strs = input.split("#");
        int[][] g = new int[13][14];
        for(int i=0,k=0;i<13;i++){
            for(int j=0;j<14;j++,k++){
                if(strs[0].charAt(k) == '1'){
                    g[i][j] = 1;
                }
            }
        }

        int meSx = Integer.parseInt(strs[1]);
        int meSy = Integer.parseInt(strs[2]);
        String meSteps = strs[3];
        int youSx = Integer.parseInt(strs[4]);
        int youSy = Integer.parseInt(strs[5]);
        String youSteps = strs[6];


        List<Cell> meCells = getCells(meSx,meSy,meSteps);
        List<Cell> youCells = getCells(youSx,youSy,youSteps);

        for(Cell c:meCells)g[c.x][c.y]=1;
        for(Cell c:youCells)g[c.x][c.y]=1;

        int[] dx = {-1,0,1,0};
        int[] dy = {0,1,0,-1};
        int[] grid_cnt = {0,0,0,0};

        int[][] new_g = new int[g.length][];

        for(int i=0;i<4;i++){
            int x = meCells.get(meCells.size()-1).x+dx[i];
            int y = meCells.get(meCells.size()-1).y+dy[i];
            if(x>=0&&x<13&&y>=0&&y<14){
                for(int k=0;k<g.length;k++){
                    new_g[k]=g[k].clone();
                }
                grid_cnt[i] = dfs(x,y,new_g);
            }
        }

        int res = 0;
        for(int i=1;i<4;i++){
            if(grid_cnt[i]>grid_cnt[res])res=i;
        }

        return res;
    }
}
